package com.zhoukai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhoukai.constants.Constants;
import com.zhoukai.dto.UserDto;
import com.zhoukai.mapper.RoleMapper;
import com.zhoukai.utils.AppMd5Utils;
import com.zhoukai.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhoukai.domain.User;
import com.zhoukai.mapper.UserMapper;
import com.zhoukai.service.UserService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * @Description TODO
 * @Author Mr.ZHOU
 * @Date Created in 2021/12/15 10:25
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public DataGridView listUserForPage(UserDto userDto) {
        Page<User> page=new Page<>(userDto.getPageNum(),userDto.getPageSize());
        QueryWrapper<User> qw=new QueryWrapper<>();
        qw.eq(userDto.getDeptId()!=null,User.COL_DEPT_ID,userDto.getDeptId());
        qw.like(StringUtils.isNotBlank(userDto.getUserName()),User.COL_USER_NAME,userDto.getUserName());
        qw.like(StringUtils.isNotBlank(userDto.getPhone()),User.COL_PHONE,userDto.getPhone());

        qw.eq(StringUtils.isNotBlank(userDto.getStatus()),User.COL_STATUS,userDto.getStatus());
        Date beginTime = userDto.getBeginTime();
        Date endTime = userDto.getEndTime();
        qw.ge(beginTime!=null, User.COL_CREATE_TIME,beginTime);
        qw.le(endTime!=null,User.COL_CREATE_TIME,endTime);
        this.userMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @Override
    public int addUser(UserDto userDto) {
        User user=new User();
        BeanUtil.copyProperties(userDto ,user);
        //设置用户类型
        user.setUserType(Constants.USER_NORMAL);
        //得到手机号的后6位
        String defaultPwd=userDto.getPhone().substring(5); //13412345678
        //设置创建者和创建时间
        user.setCreateBy(userDto.getSimpleUser().getUserName());
        user.setCreateTime(DateUtil.date());
        //设置盐
        user.setSalt(AppMd5Utils.createSalt());
        //设置密码
        user.setPassword(AppMd5Utils.md5(defaultPwd,user.getSalt(),2));
        return this.userMapper.insert(user);
    }

    @Override
    public int updateUser(UserDto userDto) {
        User user=this.userMapper.selectById(userDto.getUserId());
        if(null==user){
            return 0;
        }
        if(!userDto.getPhone().equals(user.getPhone())){
            BeanUtil.copyProperties(userDto,user);
            //得到手机号的后6位
            String defaultPwd=userDto.getPhone().substring(5); //13412345678
            //设置创建者和创建时间
            user.setCreateBy(userDto.getSimpleUser().getUserName());
            user.setCreateTime(DateUtil.date());
            //设置盐
            user.setSalt(AppMd5Utils.createSalt());
            //设置密码
            user.setPassword(AppMd5Utils.md5(defaultPwd,user.getSalt(),2));
        }else{
            BeanUtil.copyProperties(userDto,user);
        }
        user.setUpdateBy(userDto.getSimpleUser().getUserName());
        user.setUpdateTime(DateUtil.date());
        return this.userMapper.updateById(user);
    }

    @Override
    public int deleteUserByIds(Long[] userIds) {
        List<Long> ids = Arrays.asList(userIds);
        if(null!=ids&&ids.size()>0){
            //1.根据用户IDs删除sys_role_user里面的数据
            roleMapper.deleteRoleUserByUserIds(ids);
            //2,删除用户数据
            return this.userMapper.deleteBatchIds(ids);
        }
        return 0;
    }

    @Override
    public List<User> getAllUsers() {
        QueryWrapper<User> qw=new QueryWrapper<>();
        qw.eq(User.COL_STATUS,Constants.STATUS_TRUE);
        return this.userMapper.selectList(qw);
    }

    @Override
    public void resetPassWord(Long[] userIds) {
        for (Long userId : userIds) {
            User user = this.userMapper.selectById(userId);
            String defaultPwd = "";
            if (user.getUserType().equals(Constants.USER_ADMIN)) {
                defaultPwd = "123456";
            } else {
                defaultPwd = user.getPhone().substring(5);
            }
            user.setSalt(AppMd5Utils.createSalt());
            user.setPassword(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
            this.userMapper.updateById(user);
        }
    }

        @Override
    public User queryUserByPhone(String phone) {
        if (StringUtils.isBlank(phone)){
            return null;
        }else {
            QueryWrapper<User> qw=new QueryWrapper<>();
            qw.eq(User.COL_PHONE,phone);
            User user = userMapper.selectOne(qw);
            return user;

        }

    }

    @Override
    public User queryUserById(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<User> queryUsersNeedScheduling(Long userId, Long deptId) {
        QueryWrapper<User> qw=new QueryWrapper<>();
        qw.eq(deptId!=null,User.COL_DEPT_ID,deptId);
        qw.eq(userId!=null,User.COL_USER_ID,userId);
        qw.eq(User.COL_SCHEDULING_FLAG,Constants.SCHEDULING_FLAG_TRUE);
        return this.userMapper.selectList(qw);

    }


}


